package yunquan.wang.modules.base.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.base.entity.UserBasic;
import yunquan.wang.modules.base.service.RolesinfoService;
import yunquan.wang.modules.base.service.UserBasicService;

 
@Controller
@RequestMapping("/core/userbasic")
public class UserBasicController  {
	@Resource
	private UserBasicService service;
	@Resource
	private RolesinfoService rolesinfoService;
 
	@RequestMapping(value = "/list.do")
	public String list(HttpServletRequest request, Model model) {
		Pageable page = WebHelper.getPage(request);
		List<PropertyFilter> filters = WebHelper.filterRequest(request);
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,service.findPage(page, filters));
		return "core/userbasicList";
	}
 
	@RequestMapping(value = "/save.do")
	public @ResponseBody AjaxDone save(@Valid UserBasic entity, BindingResult bindingResult,String[] roleId) {
			if (bindingResult.hasErrors()) {
				return WebHelper.validFail(bindingResult);
			} else {
				try {
					entity = service.save(entity,roleId);
				} catch (Exception e) {
				return	WebHelper.saveFail( e);
				}
			}
			return WebHelper.saveSuccess();
			
	}

 
	@RequestMapping(value = "/view.do")
	public String view(String id, Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, service.get(id));
		return "core/userbasiView";
	}

 
	@RequestMapping(value = "/input.do")
	public String input(String id, Model model) {
		UserBasic entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new UserBasic();
		}
		model.addAttribute("allRoles", 	rolesinfoService.findAll());
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "core/userbasicInput";
	}
 
 
	@RequestMapping(value = "/delete.do")
	public @ResponseBody  AjaxDone delete(String[] id) {
		for (String ids : id) {
			service.delete(ids);
		}
		return WebHelper.deleteSuccess();
	}
 
}
